package cn.itcast.mapper;

import cn.itcast.entity.AddressBook;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface IAddressMapper {
    /**
     * 根据userId查询AddressBook
     * @param userId
     * @return
     */
    @Select("select * from address_book where user_id=#{userId}")
    List<AddressBook> list(Long userId);

    @Insert("insert into address_book(user_id,consignee,phone,sex,detail,label,create_time,update_time,create_user,update_user) " +
            "values(#{userId},#{consignee},#{phone},#{sex},#{detail},#{label},#{createTime},#{updateTime},#{createUser},#{updateUser})")
    void save(AddressBook addressBook);

   @Update("UPDATE address_book SET is_default =0 WHERE is_default=1")
    void removeDefaultAdd();

    @Update("update address_book set is_default=1,update_time=#{updateTime},update_user=#{updateUser} where id =#{id}")
    void updateDefaultAddr(AddressBook addressBook);

    /**
     * 根据id查询AddressBook
     * @param id
     * @return
     */
     @Select("select * from address_book where id=#{id}")
    AddressBook getById(Long id);

    @Update("UPDATE address_book SET user_id = #{userId},consignee = #{consignee},sex = #{sex}" +
            ",phone = #{phone},province_code = #{provinceCode},province_name = #{provinceName}" +
            ",city_code = #{cityCode},city_name = #{cityName},district_code = #{districtCode}" +
            ",district_name = #{districtName},detail = #{detail},label = #{label}" +
            ",is_default = #{isDefault},create_time = #{createTime},update_time = #{updateTime}" +
            ",create_user = #{createUser},update_user = #{updateUser} WHERE id = #{id}")
    void updateAddr(AddressBook addressBook);


    @Delete("delete from address_book where id=#{id}")
    void delete(Long ids);
   @Select("select * from address_book where is_default =1")
    AddressBook getDefaultAddr();
}
